PersonEntity.java
package com.ivoronline.springboot_db_h2.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class PersonEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
public String name;
public Integer age;
}
PersonRepository.java
package com.ivoronline.springboot.repository_store_entity.respositories;
import com.ivoronline.springboot.repository_store_entity.entities.PersonEntity;
import org.springframework.data.repository.CrudRepository;
public interface PersonRepository extends CrudRepository<PersonEntity, Integer> { }
MyController.java
package com.ivoronline.springboot_db_h2.controllers;
import com.ivoronline.springboot_db_h2.entities.PersonEntity;
import com.ivoronline.springboot_db_h2.repositories.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@Autowired PersonRepository personRepository;
@ResponseBody
@RequestMapping("/AddPerson")
public String addPerson() {
//CREATE PERSON
PersonEntity personEntity = new PersonEntity();
personEntity.name = "John";
personEntity.age = 20;
//STORE PERSON
personRepository.save(personEntity);
//RETURN SOMETHING
return "Hello " + personEntity.name;
}
}